// pages/myDemand/myDemand.js
var util = require('../../utils/util.js');
var wxScan = require('../../utils/wxScan.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: {
      pageNo: 1,
      pageSize: 8
    },
    demands: [],
    hidden: false,
    scrollTop: 0,
    scrollHeight: 0,
    demandsHasMore: true,
    itemName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.queryDemand();
  },
  queryDemand:function(){
    var that = this;
    wx.request({
      url: getApp().globalData.host + 'demand/list.html',
      data: { pageSize: that.data.page.pageSize, pageNo: that.data.page.pageNo,name:that.data.itemName,
      userId:getApp().globalData.platUser.userId},
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        var demands = that.data.demands;
        if (res.data.results == '' || res.data.results == null || typeof(res.data.results)=='undefined' ||res.data.results.length==0) {
          that.setData({
            demandsHasMore: false
          });
          if(demands.length>0)
            return;
        }
        for (var i = 0; i < res.data.results.length; i++) {
          res.data.results[i].createTime = util.formatDate(res.data.results[i].createTime);
          demands.push(res.data.results[i]);
        }
        that.data.page.pageNo++;
        that.setData({
          demands: demands,
          hidden: true,
          demandsHasMore:true
        });
      },
      fail: function (res) {},
      complete: function (res) {}
    })
  },
  //搜索
  inputSearch: function (e) {
    this.setData({
      itemName: e.detail.value
    })
  },
  searchItems:function(){
    this.setData({
      page: {
        pageNo: 1,
        pageSize: 8
      },
      demands: [],
      hidden: false,
      demandsHasMore: true
    });
    this.queryDemand();
  },
  scanCode:function(){
    var that = this;
    wx.scanCode({
      success: function(res){
        if("scanCode:ok"==res.errMsg){//扫描成功
          wxScan.wxScan(res.result);
        }
      }
    })
  },
  demandDetail:function(event){
    if (getApp().globalData.platUser==null){
      wx.navigateTo({
        url: '../login/login'
      });
    }else{
      var demandId = event.currentTarget.dataset.demandId;
      wx.navigateTo({
        url: '../demand/demand?demandId=' + demandId
      });
    }
  },
  addInfo:function(){
    wx.navigateTo({
      url: '../addDemand/addDemand'
    });
  },
  bindDownLoad:function(){
    var that = this;
    if (that.data.demandsHasMore) {
      that.queryDemand();
      //查询期间设置为不得再次查询
      that.setData({
        demandsHasMore: false
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})